import 'package:flutter/material.dart';
import 'bottom_navigation_bar_item_option.dart';

class TabNavigator extends StatefulWidget {
  final List<Widget> pageViewChildren;
  final Widget? child;
  final List<BottomNavigationBarItemOption> bottomNavigationBarItemOptions;
  final void Function(int)? onBottomNavigationBarTap;
  late final void Function(int) changeCurrentIndex;

  setChangeCurrentIndexFunc(func){
    changeCurrentIndex = func;
  }

   TabNavigator(
      {Key? key,
      this.pageViewChildren = const [],
      this.child,
      this.onBottomNavigationBarTap,
      required this.bottomNavigationBarItemOptions})
      : assert(pageViewChildren.length != 0 || child != null,
            '属性 pageViewChildren or child 必须其中一个有值'),
        super(key: key);

  @override
  _TabNavigatorState createState() => _TabNavigatorState();
}

class _TabNavigatorState extends State<TabNavigator> {
  Color _defaultColor = Colors.grey;
  Color _activeColor = Colors.blue;
  int _currentIndex = 0;

  late final PageController? _pageController;

  @override
  @mustCallSuper
  initState() {
    if (widget.pageViewChildren.isNotEmpty) {
      _pageController = PageController(
        initialPage: 0,
      );
    } else {
      _pageController = null;
    }
    widget.setChangeCurrentIndexFunc(changeCurrentIndex);
    super.initState();
  }

  Widget? _body() {
    if (widget.pageViewChildren.isEmpty) {
      return widget.child;
    } else {
      return PageView(
          controller: _pageController,
          physics: const NeverScrollableScrollPhysics(),
          children: widget.pageViewChildren);
    }
  }

  List<BottomNavigationBarItem> createBottomNavigationBarItems(
      List<BottomNavigationBarItemOption> bottomNavigationBarItemOptions) {
    assert(bottomNavigationBarItemOptions.length >= 2);
    return bottomNavigationBarItemOptions.asMap().entries.map((entry) {
      int index = entry.key;
      BottomNavigationBarItemOption value = entry.value;
      return BottomNavigationBarItem(
        icon: Icon(value.icon, color: _defaultColor),
        activeIcon: Icon(value.activeIcon, color: _activeColor),
        label: value.label,
        backgroundColor: _currentIndex != index ? _defaultColor : _activeColor,
      );
    }).toList();
  }

  void changeCurrentIndex(int index){
    setState(() {
      _currentIndex = index;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: _body(),
        bottomNavigationBar: BottomNavigationBar(
          currentIndex: _currentIndex,
          type: BottomNavigationBarType.fixed,
          onTap: (index) {
            _pageController?.jumpToPage(index);
            if(widget.onBottomNavigationBarTap != null){
              widget.onBottomNavigationBarTap!(index);
            }
            changeCurrentIndex(index);
          },
          items: createBottomNavigationBarItems(
              widget.bottomNavigationBarItemOptions),
          // [
          //   BottomNavigationBarItem(
          //     icon:Icon(Icons.home,color:_defaultColor),
          //     activeIcon: Icon(Icons.home,color:_activeColor),
          //     label:'首页',
          //     backgroundColor:_currentIndex != 0 ? _defaultColor:_activeColor,
          //   ),
          //   BottomNavigationBarItem(
          //     icon:Icon(Icons.search,color:_defaultColor),
          //     activeIcon: Icon(Icons.search,color:_activeColor),
          //     label:'搜索',
          //     backgroundColor:_currentIndex != 1 ? _defaultColor:_activeColor,
          //   ), BottomNavigationBarItem(
          //   icon:Icon(Icons.camera_alt,color:_defaultColor),
          //   activeIcon: Icon(Icons.camera_alt,color:_activeColor),
          //   label:'旅拍',
          //   backgroundColor:_currentIndex != 2 ? _defaultColor:_activeColor,
          // ), BottomNavigationBarItem(
          //   icon:Icon(Icons.account_circle,color:_defaultColor),
          //   activeIcon: Icon(Icons.account_circle,color:_activeColor),
          //   label:'我的',
          //   backgroundColor:_currentIndex != 3 ? _defaultColor:_activeColor,
          // )
          // ],
        ));
  }
}
